const app = getApp()
const $ = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
	clickcheall:false,
	allmoney:0,
	goosd:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  var that = this
	// 判断是否登录
	wx.getStorage({
		key:"userinfo",
		success:function(res){
			// 已登录,获取购物车信息
			wx.request({
				url:$.AdminUrl+"/YApi/getMyShopcar",
				data:{
					customerid:res.data.id
				},
				success: (res) => {
					var list = res.data.obj
					for(var i=0;i<list.length;i++){
						list[i].click = false
					}
					that.setData({
						shoplist:list,
					})
					$.shoptype = res.data.obj[0].shoptype
					console.log(list)
				}
			})
		},fail:function(fail){
			wx.navigateTo({
				url: "../login/login"
			})
		}
	})
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  var that = this
	  that.onLoad();
	  that.setData({
		  clickcheall:false,
		  allmoney:0,
		  goosd:0
	  })
	// 判断是否登录
	wx.getStorage({
		key:"userinfo",
		success:function(res){
			// 已登录,获取购物车信息
			wx.request({
				url:$.AdminUrl+"/YApi/getMyShopcar",
				data:{
					customerid:res.data.id
				},
				success: (res) => {
					var list = res.data.obj
					for(var i=0;i<list.length;i++){
						list[i].click = false
					}
					that.setData({
						shoplist:list
					})
					console.log(list)
				}
			})
		}
	})
  },
  
  // 单个点击事件
	clickche:function(e){
		var that = this
		var index = e.currentTarget.dataset.index
		var list = that.data.shoplist
		var allclick = true
		list[index].click = !list[index].click
		var allmoney = 0
		var goosd = 0
		for(var i=0;i<list.length;i++){
			if(!list[i].click){
				allclick = false
			}else{
				allmoney+=list[i].vipcashprice*list[i].count
				goosd++
			}
		}
		allclick?that.setData({clickcheall:true}):that.setData({clickcheall:false})
		that.setData({
			shoplist:list,
			allmoney:allmoney,
			goosd:goosd
		})
	},
	// 全选
	clickcheall:function(){
		var that = this
		var list = that.data.shoplist
		var allmoney = 0
		var goosd = 0
		that.setData({
			clickcheall:!that.data.clickcheall
		})
		for(var i=0;i<list.length;i++){
			list[i].click = that.data.clickcheall
			if(that.data.clickcheall){
				goosd++
				allmoney+=list[i].vipcashprice*list[i].count
			}else{
				allmoney=0
			}
		}
		that.setData({
			shoplist:list,
			allmoney:allmoney,
			goosd:goosd
		})
	},
	// 单个删除
	delthis:function(e){
		var that = this
		var thisid = e.currentTarget.dataset.id
		wx.request({
			url:$.AdminUrl+"/YApi/deleteMyShopcar",
			data:{
				shopcarids:thisid
			},
			success: (res) => {
				if(res.data.code){
					wx.showToast({
						title: res.data.msg,
						duration: 3000,
					})
					that.onShow()
				}else{
					wx.showToast({
						icon:'none',
						title: res.data.msg,
						duration: 3000,
					})
				}
				console.log(res)
			}
		})
	},
	// 多个删除
	delall:function(){
		var that = this
		var list = that.data.shoplist
		var clicklist = ","
		for(var i=0;i<list.length;i++){
			if(list[i].click){
				clicklist+=list[i].id+","
			}
		}
		wx.request({
			url:$.AdminUrl+"/YApi/deleteMyShopcar",
			data:{
				shopcarids:clicklist
			},
			success: (res) => {
				if(res.data.code){
					wx.showToast({
						title: res.data.msg,
						duration: 3000,
					})
					that.onShow()
				}else{
					wx.showToast({
						icon:'none',
						title: res.data.msg,
						duration: 3000,
					})
				}
				console.log(res)
			}
		})
	},
	// 去结算
	gojiesuan:function(){
		var that = this
		var list = that.data.shoplist
		var golist = []
		for(var i=0;i<list.length;i++){
			if(list[i].click){
				golist.push(list[i])
			}
		}
		if(that.data.goosd != 0){
			$.orderCart = golist
			wx.navigateTo({
				url:"../payment/payment?gowuce=1"
			})
		}else{
			wx.showToast({
				icon: 'none',
				title:'你还没选择商品',
				duration: 3000,
			})
		}
	},
	// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
	
})